package client.messages;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.GregorianCalendar;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import data.Invite;
import data.Message;
import data.PropertyChangeSpeaker;

import miscellaneous.Storage;
import miscellaneous.TimeSupport;
import mockup.MessageMockup;
import mockup.Mockup;
import mockup.MockupFactory;

import java.awt.BorderLayout;
import java.awt.Color;

public class MessagePane extends JPanel implements PropertyChangeListener{
	
	private DefaultListModel listModel;
	private JList list;
	
	public MessagePane() {
		setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane);
		
		listModel = new DefaultListModel();
		list = new JList(listModel);
		scrollPane.setViewportView(list);
		list.setCellRenderer(new MessageListRenderer());
		list.addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if(!e.getValueIsAdjusting()) {
					Object selected = list.getSelectedValue();
					if(selected instanceof Invite){
						Invite invite = (Invite)selected;
						InviteOverview io = new InviteOverview();
						io.setVisible(true);
						io.pack();
						if(invite.getReadTime() == null || invite.getReadTime().getTimeInMillis() == 0 ) {
							invite.setReadTime(new GregorianCalendar());
						}
					}
					if(selected instanceof Message){
						Message message = (Message) selected;
//						String out = "Meldingsbeskrivelse: " + message.getMeeting().getDescription() +
//								"\n\nTittel: " + message.getMeeting().getTitle() +
//								"\nStart: " + TimeSupport.showTimeNice(message.getMeeting().getReservation().getStartTime())
//								+ " " + TimeSupport.showDateNice(message.getMeeting().getReservation().getStartTime()) +
//								"\nSlutt: " + TimeSupport.showTimeNice(message.getMeeting().getReservation().getEndTime())
//								+ " " + TimeSupport.showDateNice(message.getMeeting().getReservation().getEndTime());
//						
//						JOptionPane.showMessageDialog(null, message.toString() , "Angående: " + message.getSubject() , 
//								JOptionPane.INFORMATION_MESSAGE);
						if(message.getReadTime() == null || message.getReadTime().getTimeInMillis() == 0 ) {
							message.setReadTime(new GregorianCalendar());
						}	
					}
					list.getSelectionModel().clearSelection();
					
				}
			}
			
		});
		
		Storage.messages.addPropertyChangeListener(this);
		update();
	}

	
	private void update() {
		listModel.clear();
		
		for(Message message: Storage.messages) {
			if(message.getReceiver() == Storage.getloggedInUser())
				listModel.addElement(message);
		}
		list.updateUI();
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if(evt.getSource() == Storage.messages && evt.getPropertyName().equals("addObject"))
			if(evt.getNewValue() instanceof PropertyChangeSpeaker)
				((PropertyChangeSpeaker)evt.getNewValue()).addPropertyChangeListener(this);
		update();
		
	}
	
}
